//
//  MTNoDataView.swift
//  MT
//
//  Created by zhang qiang on 2018/12/9.
//  Copyright © 2019年 张强. All rights reserved.
//

import UIKit

public class NoDataView: UIView {

    public static func defaultNoDataView(title: String = "暂时没有数据") -> NoDataView {
        
        return NoDataView(title: title, image: UIImage.imagePath(named: "default_noData", bundle: Bundle.MTCategroy)!)
    }
    
    private let imageView = UIImageView()
    private let flushBtn = UIButton(type: .custom)
    
    private var _action: (()->Void)?
    public var action: (()->Void)? {
        get {
            return nil
        }
        set {
            _action = newValue
            if let _ = _action {
                flushBtn.isHidden = false
            }
        }
    }
    
    private let label = UILabel().then {
        $0.textColor = UIColor.init(hex: 0x666666)
        $0.font = HelveticaNeueFont(14.0)
        $0.textAlignment = .center
        $0.numberOfLines = 0
    }
    
    public init(title: String, image: UIImage){
        super.init(frame: CGRect.zero)
        imageView.image = image
        label.text = title
        label.font = PingFangSCRegularFont(14)
        label.textColor = UIColor(hex: 0x979797)
        self.backgroundColor = MainPageBackgroundColor
        
        self.addSubview(imageView)
        self.addSubview(label)
        
        imageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(56.0 + StatusBarHeight + NavigationBarHeight)
        }
        
        label.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(imageView.snp.bottom).offset(54)
            make.left.greaterThanOrEqualTo(self).offset(MainSubviewHorMargin)
            make.right.lessThanOrEqualTo(self).offset(0-MainSubviewHorMargin)
        }
        
        self.addSubview(flushBtn)
        flushBtn.setTitle("刷新", for: .normal)
        flushBtn.setTitleColor(UIColor(hex: 0xFF6B7B), for: .normal)
        flushBtn.layer.borderColor = UIColor(hex: 0xFF6B7B).cgColor
        flushBtn.layer.borderWidth = 0.5
        flushBtn.layer.masksToBounds = true
        flushBtn.layer.cornerRadius = 16
        flushBtn.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(label.snp.bottom).offset(14)
            make.width.equalTo(80)
            make.height.equalTo(32)
        }
        flushBtn.addTarget(self, action: #selector(flushAction), for: .touchUpInside)
        flushBtn.isHidden = true
    }
    
    @objc
    private func flushAction() {
        _action?()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
